package com.dt.app.controller.product;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dt.app.api.product.service.ICategoryService;
import com.dt.app.api.product.service.IProductService;
import com.dt.app.modules.product.entities.Category;
import com.dt.app.modules.product.entities.Product;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateFormatUtils;
import javax.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.ArrayList;
import com.dt.app.common.response.ResultUtil;
import com.dt.app.modules.product.entities.ProductGg;
import com.dt.app.modules.product.vo.ProductGgBean;
import com.dt.app.api.product.service.IProductGgService;
import com.dt.app.controller.base.BaseController;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author 鸿源塔基
 * @since 2024-03-11
 */
@RestController
@RequestMapping("api/v1/product/productGg")
public class ProductGgController extends BaseController{
    @Autowired
    private IProductGgService productGgService;
    @Autowired
    @Lazy
    private IProductService productService;
    @Autowired
    @Lazy
    private ICategoryService categoryService;

    @ApiOperation(value = "列表")
    @RequestMapping("list")
    public ResultUtil<IPage<ProductGgBean>> list(ProductGgBean request){
        QueryWrapper<ProductGg> wrapper = new QueryWrapper<>();
        Product product=null;
        Category category=null;
        if(null != request.getProductId() ) {
            wrapper.eq("product_id",request.getProductId());
            product=productService.getById(request.getProductId());
            category=categoryService.getById(product.getCategoryId());
        }
        wrapper.eq("company_id",this.getSysCompanyId());
        wrapper.orderByDesc("gg_level");
        Page page = new Page<>(request.getCurrent(),request.getSize());
        IPage listBody = productGgService.page(page,wrapper);
        List<ProductGg> entityList=listBody.getRecords();
        if(!entityList.isEmpty()) {
            List<ProductGgBean> responseList = new ArrayList<>();
            for (ProductGg it:entityList){
                ProductGgBean bean = new ProductGgBean();
                BeanUtils.copyProperties(it,bean);
                responseList.add(bean);
            }
            listBody.setRecords(responseList);
        }
        ResultUtil resultUtil = ResultUtil.success(listBody);
        if(product!=null)resultUtil.getOtherKV().put("productIdName",product.getPname());
        if(category!=null)resultUtil.getOtherKV().put("categoryIdName",category.getCname());
        return resultUtil;
    }

    @PostMapping("save")
    @ApiOperation(value = "新增")
    public ResultUtil save(@RequestBody @Valid ProductGg entity){
        //entity.setCreateTime(LocalDateTime.now());entity.setDeleted(0);
        entity.setDeleted(0);
        entity.setCompanyId(this.getSysCompanyId());
        productGgService.save(entity);
        return ResultUtil.success();
    }

    @GetMapping("getById/{id}")
    @ApiOperation(value = "id查询")
    public ResultUtil<ProductGgBean> getById(@PathVariable String id){
        ProductGg entity = productGgService.getById(id);
        if(entity!=null){
            ProductGgBean bean = new ProductGgBean();
            BeanUtils.copyProperties(entity,bean);
            return ResultUtil.success(bean);
        }
        return ResultUtil.error("没有找到对象");
    }

    @PutMapping("update")
    @ApiOperation(value = "修改")
    public ResultUtil update(@RequestBody @Valid ProductGg entity){
        productGgService.updateById(entity);
        return ResultUtil.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("delete/{id}")
    public ResultUtil delete(@PathVariable Long id){
        productGgService.removeById(id);
        return ResultUtil.success();
    }
}